
//
//  Line.m
//  hanafudaIPhone
//
//  Created by 우성 김 on 12. 3. 26..
//  Copyright (c) 2012년 yy01ar@gmail.com. All rights reserved.
//

#import "Line.h"
#import "Card.h"

@implementation Line
-(id)initWithLineNo:(int)initLineNo
{
    if(self = [super init])
    {
        cardArray = [[NSMutableArray alloc]init];
        enabled = YES;
        
        lineNo = initLineNo;
//        positionY =
    }
    return self;
}

// 뗄 수 있는지 확인 
-(BOOL)canPop
{
    // 9, 19, 29로 뗄 수 있는지 확인한다.
    Card* card0 = [cardArray objectAtIndex:0];
    Card* card1 = [cardArray objectAtIndex:1];
    Card* cardLast2 = [cardArray objectAtIndex:cardArray.count-3];
    Card* cardLast1 = [cardArray objectAtIndex:cardArray.count-2];
    Card* cardLast = [cardArray objectAtIndex:cardArray.count-1];
    
    int type1 = card0.value + card1.value + cardLast.value;
    if(type1 == 9 || type1 == 19 || type1 == 29) return YES;
    
    int type2 = card0.value + cardLast1.value + cardLast.value;
    if(type2 == 9 || type2 == 19 || type2 == 29) return YES;
    
    int type3 = cardLast2.value + cardLast1.value + cardLast.value;
    if(type3 == 9 || type3 == 19 || type3 == 29) return YES;
    
    return NO;
}

// 카드를 떼낸다.
-(NSMutableArray*)popCard
{
    // 9, 19, 29로 뗄 수 있는지 확인한다.
    Card* card0 = [cardArray objectAtIndex:0];
    Card* card1 = [cardArray objectAtIndex:1];
    Card* cardLast2 = [cardArray objectAtIndex:cardArray.count-3];
    Card* cardLast1 = [cardArray objectAtIndex:cardArray.count-2];
    Card* cardLast = [cardArray objectAtIndex:cardArray.count-1];
    
    int type1 = card0.value + card1.value + cardLast.value;
    if(type1 == 9 || type1 == 19 || type1 == 29) {
        return [[NSMutableArray alloc]initWithObjects:card0, card1, cardLast, nil];
    }
    
    int type2 = card0.value + cardLast1.value + cardLast.value;
    if(type2 == 9 || type2 == 19 || type2 == 29) {
        return [[NSMutableArray alloc]initWithObjects:card0, cardLast1, cardLast, nil];
    }
    
    int type3 = cardLast2.value + cardLast1.value + cardLast.value;
    if(type3 == 9 || type3 == 19 || type3 == 29) {
        return [[NSMutableArray alloc]initWithObjects:cardLast2, cardLast1, cardLast, nil];
    }
    
    return nil;
}

// 카드를 놓을 수 있는지 확인
-(BOOL)isEnable
{
    return enabled;
}

// 라인에 카드를 추가한다
-(void)addCard:(Card*) card
{
    [cardArray addObject:card];
}
@end
